﻿using System;
using System.Collections.Generic;
using System.Text;
using Gtk;

namespace GtkSharpTutorial
{
    class ShareAdjument
    {
        Adjustment adjustment;
        Scale hscale;
        Scale vscale;
        Scrollbar scrollbar;
        CheckButton button;
        Label adjustmentValue;
        Entry scrollPosition;

        public Box Widget { get; }

        void scale_set_default_values(Scale s)
        {
            //s.UpdatePolicy = UpdateType.Continuous;
            s.Digits = 1;
            s.ValuePos = PositionType.Top;
            s.DrawValue = true;
        }

        public ShareAdjument()
        {
            /* value, lower, upper, step_increment, page_increment, page_size */
            /* Note that the page_size value only makes a difference for
             * scrollbar widgets, and the highest value you'll get is actually
             * (upper - page_size). */
            adjustment = new Adjustment(0.0, 0.0, 101.0, 0.1, 1.0, 1.0);
            adjustment.ValueChanged += (s, e) =>
                adjustmentValue.Text = adjustment.Value.ToString();

            hscale = GetHScale();
            vscale = GetVScale();
            scrollbar = GetScrollbar();
            button = GetButton();

            var main = new VBox(false, 0);
            main.PackStart(GetRanges(), true, true, 0);
            main.PackStart(GetLables(), false, false, 0);
            main.PackStart(button, true, true, 0);
            main.PackStart(GetSetPosition(), true, true, 0);
            main.PackStart(GetSetUpdatePolicy(), true, true, 0);

            main.ShowAll();
            Widget = main;
        }

        private Box GetSetUpdatePolicy()
        {
            var box8 = new HBox(false, 10);
            box8.BorderWidth = 10;

            /* Yet another option menu, this time for the update policy of the
             * scale widgets */
            var label = new Label("Scale Update Policy:");
            box8.PackStart(label, false, false, 0);
            label.ShowAll();
            return box8;
        }

        private Box GetSetPosition()
        {
            var box = new HBox(false, 10);
            box.BorderWidth = 10;

            /* An option menu to change the position of the value */
            var label = new Label("Scale Value Position:");
            box.PackStart(label, false, false, 0);

            box.PackStart(GetTextBox(), false, false, 0);


            var button = new Button("改变");
            button.Clicked += (s, e) =>
            {
                if (int.TryParse(scrollPosition.Buffer.Text, out int val))
                    adjustment.Value = val;
            };
            box.PackStart(button, false, false, 0);

            return box;
        }

        Scale GetVScale()
        {
            var scale = new VScale(adjustment);
            scale_set_default_values(scale);
            scale.DrawValue = false;
            return scale;
        }

        Scale GetHScale()
        {
            var scale = new HScale(adjustment);
            scale_set_default_values(scale);
            scale.DrawValue = false;
            return scale;
        }

        Scrollbar GetScrollbar()
        {
            /* reuse the same adjustment again */
            var scrollbar = new HScrollbar(adjustment);

            /* Notice how this causes the scales to always be updated
             * continuously when the scrollbar is moved */
            //scrollbar.UpdatePolicy = UpdateType.Continuous;

            return scrollbar;
        }

        CheckButton GetButton()
        {
            /* A checkbutton to control whether the value is displayed or not */
            var button = new CheckButton("Display value on scale widgets");
            button.Toggled += (s, e) =>
            {
                hscale.DrawValue = button.Active;
                vscale.DrawValue = button.Active;
            };
            return button;
        }

        Box GetRanges()
        {
            var box2 = new HBox(false, 0);
            box2.BorderWidth = 10;
            var box3 = new VBox(false, 10);
            box2.PackStart(box3, true, true, 0);

            var box4 = new VBox(false, 10);
            box2.PackStart(box4, true, true, 0);

            box3.PackStart(vscale, true, true, 0);
            box4.PackStart(hscale, true, true, 0);
            box4.PackStart(scrollbar, true, true, 0);

            return box2;
        }

        Box GetLables()
        {
            var box = new HBox(false, 10);
            box.BorderWidth = 10;
            var left = new VBox();
            var right = new VBox();
            box.PackStart(left, false, false, 0);
            box.PackStart(right, false, false, 0);

            BandingValueLable(left, right);

            return box;
        }

        void BandingValueLable(Box left, Box right)
        {
            var label = new Label("Value:");
            adjustmentValue = new Label("");
            left.PackStart(label, false, false, 0);
            right.PackStart(adjustmentValue, false, false, 0);
        }

        Box GetTextBox()
        {
            var box = new VBox(false, 8);
            var fillBox = new VBox();

            scrollPosition = new Entry();
            scrollPosition.MaxLength = 5;
            box.PackStart(fillBox, false, false, 0);
            box.PackStart(scrollPosition, true, true, 0);

            return box;
        }
    }
}
